﻿package com.ufenlei.LTS{
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	import flash.events.TimerEvent;

	public class ScrolledBlock extends Block{
		private var _scroll:Scroll;
		private var _toY:int;
		private var _stopmove:Boolean = false;
		public function ScrolledBlock(w:uint = 400,h:uint=300){
			super(w,h);
			_scroll = new Scroll(this.bHeight);
			_scroll.x = this.bWidth - 20;
			_scroll.addEventListener(Scroll.UPDATE, onScrollUpdate);
			addChild(_scroll);
			updateScroll();			
			this.addEventListener(Event.CHANGE,onChange );
			this.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);
			this._container.addEventListener(Input.CHANGE, onContainerChange,true);
			if(1){
				var topS:BodyShadow = new BodyShadow(0,this.bWidth-20);
				var bottomS:BodyShadow = new BodyShadow(1,this.bWidth-20);
				topS.y = 0;
				topS.x = 0;
				bottomS.x = 0;
				bottomS.y = this.bHeight - 5;
				this.addChild(topS);
				addChild(bottomS); 
			}
		}
		public function updateScroll():void{
			if(trueHeight > bHeight){
				var d:int = trueHeight - bHeight;				
				_scroll.barHeight = bHeight;
				_scroll.scrollHeight = d;
			}else{
				_scroll.barHeight = bHeight;
				_scroll.scrollHeight = 0;
			}
		}
		private function onContainerChange(evt:Event):void{
			trace("containerChange", evt.target is TestingLibItem, evt.eventPhase);
			this.getTrueHeight();
			updateScroll();
		}
		private function onWheel(evt:MouseEvent):void{
			_stopmove = true;
			_scroll.moveBy(evt.delta * -6);
		}
		private function onScrollUpdate(evt:ProgressEvent):void{
			//trace(trueHeight,bHeight);
			//trace(evt);
			if(trueHeight > bHeight){
				this._container.y = -1 * (trueHeight - bHeight ) * evt.bytesLoaded / evt.bytesTotal;
			}
		}
		private function onChange(evt:Event):void{
			if(evt.target is ScrolledBlock){
				updateScroll();
			}			
		}
		protected function get pecent():Number{
			if(trueHeight > this.bHeight){
				return this._container.y / (this.bHeight - this.trueHeight);
			}else{
				return 0;
			}		
		}
		public function scrollTo(pct:Number,noEvent:Boolean = false):void{
			this._scroll.scrollTo(pct,noEvent);
		}
		public function moveTo(target:Sprite):void{
			if(this.trueHeight > this.bHeight && (trueHeight - bHeight) > -_container.y){
				this._toY = Math.max(-target.y, this.bHeight-this.trueHeight);
				var timer:Timer = new Timer(50);
				timer.addEventListener(TimerEvent.TIMER, makeMove);
				timer.start();
			}
		}
		private function makeMove(evt:TimerEvent):void{
			if(!_stopmove){
				if(_container.y > _toY + 50 ){
					_container.y -= 50;
				}else if(_container.y< _toY -50){
					_container.y += 50;
				}else{
					this._container.y = _toY;
					(evt.target as Timer).removeEventListener(TimerEvent.TIMER, makeMove);
					_stopmove = false;
				}
			}else{
				(evt.target as Timer).removeEventListener(TimerEvent.TIMER, makeMove);
				_stopmove = false;
			}
			
			scrollTo(pecent,true);
		}
	}
}
import flash.display.GradientType;
import flash.geom.Matrix;
import flash.display.Sprite;

class BodyShadow extends Sprite{
	public function BodyShadow(type:uint, w:uint, h:uint = 5){
		var colors:Array = new Array(0x555555,0x555555);
		var ratios:Array;
		var alphas:Array;
		var m:Matrix = new Matrix();
		m.createGradientBox(w, h, Math.PI/2);
		var a0:Number = 0.5;
		var a1:Number = 0;
		if(type == 0 ){
			ratios = new Array(0,255);
			alphas = new Array(a0,a1);
		}else{
			ratios = new Array(0,255);
			alphas = new Array(a1,a0);
		}
		this.graphics.beginGradientFill (GradientType.LINEAR,colors,alphas,ratios,m);
		this.graphics.drawRect(0, 0, w, h);
		this.graphics.endFill();
	}
}